﻿using System;
using System.IO;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.SilverlightControls;
using System.Drawing;
using System.Text;
using System.Xml.Serialization;
using System.Collections.Generic;

using SBPweb.PROvibe.Server.Framework.Actors;
using SBPweb.PROvibe.Server.Framework.Actors.Parameters;
using SBPweb.PROvibe.Server.Framework.Session;
using SBPweb.PROvibe.Server.Framework.Contracts;
using SBPweb.PROvibe.Server.Framework.Contracts.Parameters;

using SBPweb.PROvibe.Server.Framework.Utilities.Collections;

[assembly:TagPrefix("SBPweb.PROvibe.Server.Host.Controls", "PROvibe")]
namespace SBPweb.PROvibe.Server.Framework.Controls
{

    public partial class SilverlightContainer : Silverlight
    {

        #region Private fields

        private EntryInitializationParametersDictionary entryParams = new EntryInitializationParametersDictionary();

        #endregion

        #region Constructor logic

        public SilverlightContainer()
        {
            InitializeComponent();
            InitializeHandlers();
            InitializeParameters();
        }

        private void InitializeComponent()
        {
            this.Width = new Unit("100%");
            this.Height = new Unit("100%");
            this.Windowless = true;
            this.HtmlAccess = HtmlAccess.Enabled;
            this.BackColor = Color.Transparent;
            this.PluginBackground = Color.Transparent;
        }

        private void InitializeHandlers()
        {
            //entryParams.InitializationBegin += new EventHandler(entryParams_InitializationBegin);
            //entryParams.InitializationEnd += new EventHandler(entryParams_InitializationEnd);
            //entryParams.ItemAdded += new EventHandler<DictionaryItemEventArgs<string, IEntryInitializationParameter>>(entryParams_ItemAdded);
            //entryParams.ItemChanged += new EventHandler<DictionaryItemEventArgs<string, IEntryInitializationParameter>>(entryParams_ItemChanged);
            //entryParams.ItemRemoved += new EventHandler<DictionaryItemEventArgs<string, IEntryInitializationParameter>>(entryParams_ItemRemoved);
        }

        private void InitializeParameters()
        {
            string[] pars = new string[]{
                string.Format("SessionId={0}", SessionManager.Session.SessionIdentifier),
                string.Format("ClientIp={0}", IPAddress.Parse(HttpContext.Current.Request.UserHostAddress).ToString()),
                string.Format("ServerProcessedTime={0}", DateTime.Now.ToFileTimeUtc())
            };
            StringBuilder sb = new StringBuilder();
            foreach(string p in pars)
            {
                if(sb.Length > 0)
                {
                    sb.Append(",");
                }
                sb.Append(p);
            }
            InitParameters = sb.ToString();
            //entryParams.BeginInitialization();
            //OnDefaultParameterInitializing();
            //entryParams.EndInitialization();
        }

        protected virtual void OnDefaultParameterInitializing()
        {
            //entryParams.Add("SessionId", new EntryInitializationParameter(SessionManager.Session.SessionIdentifier));
            //entryParams.Add("ClientIp", new EntryInitializationParameter(IPAddress.Parse(HttpContext.Current.Request.UserHostAddress).ToString()));
            //entryParams.Add("ServerProcessedTime", new EntryInitializationParameter<DateTime>(DateTime.Now));
        }

        #endregion

        #region Property accessors

        public EntryInitializationParametersDictionary EntryParameters
        {
            get
            {
                return entryParams;
            }
        }

        #endregion

        #region Methods

        private void SerializeToInitParams()
        {
            InitParameters = EntryInitializationParametersDictionary.Serialize(entryParams);
        }

        #region Event handlers

        private void entryParams_InitializationBegin(object sender, EventArgs e)
        {
        }

        private void entryParams_InitializationEnd(object sender, EventArgs e)
        {
            SerializeToInitParams();
        }

        private void entryParams_ItemAdded(object sender, DictionaryItemEventArgs<string, IEntryInitializationParameter> e)
        {
        }

        private void entryParams_ItemChanged(object sender, DictionaryItemEventArgs<string, IEntryInitializationParameter> e)
        {
        }

        private void entryParams_ItemRemoved(object sender, DictionaryItemEventArgs<string, IEntryInitializationParameter> e)
        {
        }

        #endregion

        #endregion

    }

}
